import 'package:ai_desktop_eidtor/common/y_text.dart';
import 'package:ai_desktop_eidtor/util/string_util.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../routes/app_page.dart';
import 'book_detail_logic.dart';
import 'book_detail_state.dart';

class BookDetailPage extends StatelessWidget {
  BookDetailPage({Key? key}) : super(key: key);
  final BookDetailLogic logic = Get.put(BookDetailLogic());
  final BookDetailState state = Get.find<BookDetailLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: YText(text: '书籍详情')),
      body: GetBuilder<BookDetailLogic>(
        builder: (_) => Column(
          children: [
            Expanded(
              child: Container(
                padding: EdgeInsets.only(left: 20, right: 20),
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      _headerWidget(),
                      hSpace(10),
                      _firstChapter(),
                      hSpace(10),
                      _lastChapter(),
                      hSpace(10),
                      _introWidget(),
                    ],
                  ),
                ),
              ),
            ),
            Row(
              children: [
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      logic.deleteBook();
                    },
                    child: Container(
                      padding: EdgeInsets.all(10),
                      alignment: Alignment.center,
                      color: Colors.grey.shade500,
                      child: YText(text: '删除该书籍', color: Colors.white),
                    ),
                  ),
                ),
                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      Get.toNamed(
                        RouteName.editor,
                        arguments: {
                          'bookId': logic.bookId,
                          'title': logic.bookModel?.title,
                        },
                      )?.then((v){
                        logic.loadChapter(canLoad: true);
                      });
                    },
                    child: Container(
                      padding: EdgeInsets.all(10),
                      alignment: Alignment.center,
                      color: Colors.blue,
                      child: YText(text: '开始编辑', color: Colors.white),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _firstChapter() {
    return GestureDetector(
      onTap: (){
        Get.toNamed(
          RouteName.editor,
          arguments: {
            'bookId': logic.bookId,
            'title': logic.bookModel?.title,
          },
        )?.then((v){
          logic.loadChapter(canLoad: true);
        });
      },
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(5),
          color: Colors.blue,
        ),
        padding: EdgeInsets.all(10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Icon(Icons.book, color: Colors.white),
                wSpace(10),
                YText(text: logic.bookModel?.title, color: Colors.white),
              ],
            ),
            Row(
              children: [
                YText(
                  text: "共${logic.chapterModel?.chapterNumber ?? 0}章",
                  color: Colors.white,
                ),
                wSpace(10),
                Icon(Icons.chevron_right_sharp, color: Colors.white),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _headerWidget() {
    return Row(
      children: [
        Container(
          height: 120,
          width: 100,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            color: Colors.red,
          ),
        ),
        wSpace(10),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              YText(text: logic.bookModel?.title),
              hSpace(10),
              YText(text: '作者:${logic.bookModel?.author}'),
              hSpace(10),
              YText(text: '创建时间:${logic.bookModel?.createdAt}'),
              hSpace(10),
              YText(text: '最近编辑时间:${logic.bookModel?.updatedAt}'),
            ],
          ),
        ),
      ],
    );
  }

  Widget _introWidget() {
    return Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(top: 12, bottom: 12),
      width: MediaQuery.of(Get.context!).size.width,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          YText(text: '书籍简介:',fontWeight: FontWeight.bold),
          hSpace(10),
          YText(
            maxLines: 10000,
            text: logic.bookModel?.description,
            textAlign: TextAlign.left,
          ),
        ],
      ),
    );
  }

  Widget _lastChapter() {
    return Container(
      padding: EdgeInsets.only(left: 20, right: 20, top: 12, bottom: 12),
      width: MediaQuery.of(Get.context!).size.width,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        color: Colors.grey.shade300,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          YText(text: logic.chapterModel?.title),
          hSpace(10),
          YText(
            text: logic.chapterModel?.content,
            maxLines: 5,
            textAlign: TextAlign.left,
            overflow: TextOverflow.ellipsis,
          ),
        ],
      ),
    );
  }
}
